<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/7
 * Time: 18:45
 */

namespace App\Process;

use App\Enums\UrlEnums;
use App\Library\Auth;
use App\Utility\Curl;
use App\Utility\Pool\MysqlObject;
use App\Utility\Pool\MysqlPool;
use EasySwoole\Component\Process\AbstractProcess;
use EasySwoole\EasySwoole\Logger;
use EasySwoole\MysqliPool\Mysql;
use EasySwoole\RedisPool\Redis;
use Swoole\Coroutine;

class LeagueScore extends AbstractProcess
{
    public function run($arg)
    {
        $consumeKey = 'league_id';
        go(function ()use($arg,$consumeKey){
            Redis::invoker('redis',function (\Swoole\Coroutine\Redis $redis)use($consumeKey,$arg){
                $redis->delete($consumeKey);
                while (1){
                    $league_id = $redis->lPop($consumeKey);
                    if( empty($league_id) ){
                        Coroutine::sleep(5);
                        continue;
                    }
                    list($s1, $s2) = explode(' ', microtime());
                    $microtime = (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000); //毫秒时间戳
                    $option = [
                        'path'      => UrlEnums::LOL_LEAGUE_BOARD_PATH,
                        'api_time'  => $microtime
                    ];
                    $params = [
                        'query'     => [
                            'league_id' => $league_id,
                            'version'   => 2
                        ],
                        'header'    => Auth::getInstance()->header($option),
                    ];
                    $response = Curl::request('GET',UrlEnums::FJURL.UrlEnums::LOL_LEAGUE_BOARD_PATH,$params);
                    $data = $response->getBody();
                    $data = json_decode($data,true);
                    if( empty($data)  ){
                        $redis->rPush($consumeKey,$league_id);
                        Coroutine::sleep(2);
                        Logger::getInstance()->console("重新进入队列{$league_id}");
                        continue;
                    }
                    if( $data['code'] == 200 && !empty($data['data']) ){
                        MysqlPool::invoke(function (MysqlObject $db)use($data,$league_id,$arg){
                            foreach ( $data['data'] as $item ){
                                $res = $db->where('league_id',$league_id)->where('team_id',$item['team_id'])->getOne($arg['table']);
                                if( !$res ){
                                    $db->insert($arg['table'],$item);
                                }else{
                                    $db->where('league_id',$league_id)->where('team_id',$item['team_id'])->update($arg['table'],[
                                        'win_count' => $item['win_count'],
                                        'lost_count'=> $item['lost_count'],
                                        'score'     => $item['score'],
                                        'stage'     => $item['stage']
                                    ]);
                                }
                            }
                        });
                    }
                }
            });
        });
    }
}